package com.cuit.xsc.weishare.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Bundle;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.cuit.xsc.weishare.R;
import com.cuit.xsc.weishare.util.ConfigUtil;
import com.cuit.xsc.weishare.util.LogUtil;




/**
 * 用户授权页面
 * 		1.初始化OAuth对象
 * 		2.获取用户授权页面并填充至webView
 * 		3.根据载入的url判断匹配规则的结果执行跳�? * 
 * @author bywyu
 *
 */
public class WebViewAct extends BaseActivity {

	private String url;
    
	@Override
    protected void onCreate(Bundle savedInstanceState)
	{
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.authorization_ui);
	    url = getIntent().getStringExtra("url");
	    LogUtil.d(this, "url="+url);
	   
    }
	
	@Override
	protected void onResume()
	{
		super.onResume();
		 initWebView(url);
	}
	
	private void initWebView(String url) {
		WebView authorizationView = (WebView) findViewById(R.id.authorizationView);
	    authorizationView.clearCache(true);
	    authorizationView.getSettings().setJavaScriptEnabled(true);
	    authorizationView.getSettings().setSupportZoom(true);
	    authorizationView.getSettings().setBuiltInZoomControls(true);
	    authorizationView.setWebViewClient(new WebViewC()); 
	    authorizationView.loadUrl(url);
    }

	class WebViewC extends WebViewClient{
		private int index = 0;
		@Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

			view.loadUrl(url);
			return true;
        }
		
		/**
		 * 由于腾讯授权页面采用https协议
		 * 		执行此方法接受所有证�?		 */
		public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
			 handler.proceed() ;
		 }

		@Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
	        super.onPageStarted(view, url, favicon);
	        LogUtil.d(this, "onPageStarted url = "+url );
	       // LogUtil.d(this, "url = "+url);
	        
	        /**
	         *  url.contains(ConfigUtil.callBackUrl)
	         *  如果授权成功url中包含之前设置的callbackurl
	         *  		包含：授权成�?	         *
	         *index == 0
	         *由于该方法onPageStarted可能被多次调用�?成重复跳�?	         *		则添加此标示
	         */
	        
	        if( url.contains(ConfigUtil.callBackUrl) && index == 0){
	        	//Log.e(TAG, "url--"+url);
	        	index ++;
	        	Uri uri = Uri.parse(url);
	        	String oauth_verifier = uri.getQueryParameter("oauth_verifier");
	        	LogUtil.i(this, "oauth_verifier="+oauth_verifier);	        		        		        	
	        		    	        	
            	Intent intent = new Intent(WebViewAct.this,SuccessAct.class);
            	intent.putExtra("url", url);
            	WebViewAct.this.startActivity(intent);
            	WebViewAct.this.finish();
           }
        }

		@Override
        public void onPageFinished(WebView view, String url) {
	        // TODO Auto-generated method stub
	        super.onPageFinished(view, url);
        }
	}

}
